package xjtu.se.articlecommunity.user.home.impl;

import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Transactional
@Data
public class HtmlHandler {

    public String HtmlStrHandle(String htmlStr) {
        String paraEx="<p[^>]+>";
        String regEx_html="<[^>]+>";

        Pattern p1_html=Pattern.compile(paraEx,Pattern.CASE_INSENSITIVE);
        Matcher m1_html=p1_html.matcher(htmlStr);
        htmlStr=m1_html.replaceAll(" ");

        Pattern p2_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m2_html=p2_html.matcher(htmlStr);
        htmlStr=m2_html.replaceAll("");

        if(htmlStr.length()>200){
            htmlStr=htmlStr.substring(0,199);
            htmlStr+="...";
        }
        return  htmlStr;
    }

    public String[] HtmlImgHandle(String htmlStr){
        String[] tmplist = htmlStr.split("img src=\"");
        String[] imglist = new String[tmplist.length-1>5?5:tmplist.length-1];
        if(tmplist.length>=1){
            for(int i=1;i<tmplist.length && i<5;i++){
                String img = tmplist[i].split("\"")[0];
                imglist[i-1] = img;
            }
        }
        return imglist;
    }

}
